package com.czk.linkedList;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/6/11 21:20
 */
public class SingleLinkedListTest {
    public static void main(String[] args) {
        HeroNode heroNode=new HeroNode("詹姆斯",35,"皇帝");
        HeroNode heroNode1=new HeroNode("韦德",37,"闪电侠");
        HeroNode heroNode2=new HeroNode("波什",36,"龙王");
        SingleLinkedList singleLinkedList=new SingleLinkedList();
        singleLinkedList.add(heroNode);
        singleLinkedList.add(heroNode1);
        singleLinkedList.add(heroNode2);
        singleLinkedList.list();
        singleLinkedList.remove(heroNode2);
        singleLinkedList.list();
        // 测试二
        SingleLinkedList singleLinkedList1=new SingleLinkedList();
        singleLinkedList1.addOrderByNum(heroNode1);
        singleLinkedList1.addOrderByNum(heroNode);
        singleLinkedList1.addOrderByNum(heroNode2);
        singleLinkedList1.list();
    }
}
//
class SingleLinkedList{
    // 定义一个头结点，并不存放具体的数据
    private HeroNode heroNode=new HeroNode("",0,"");
    // 添加节点
    public void add(HeroNode heroNode){
        HeroNode temp=this.heroNode;
        // 判断链表最后一位是否为空
        while (true){
            if (temp.getNext()==null){
                temp.setNext(heroNode);
                break;
            }
            temp=temp.getNext();
        }
    }
    // 遍历链表
    public void list(){
        if (heroNode.getNext()==null) return;
        HeroNode temp=this.heroNode;
        while (true){
            if (temp.getNext()==null) break;
            System.out.println(temp.getNext());
            temp=temp.getNext();
        }
    }
    // 移除元素
    public void remove(HeroNode heroNode){
        // 得到该元素
        if (this.heroNode.getNext()==null) {
            System.out.println("链表为空，移除失败");
            return;
        }
        // 寻找链表
        HeroNode temp=this.heroNode;
        while (true){
            if (heroNode.equals(temp.getNext())){
                // 前一节点的指针指向下一结点
                temp.setNext(temp.getNext().getNext());
                break;
            }
            temp=temp.getNext();
        }
    }
    // 添加司通过number排序
    public void addOrderByNum(HeroNode heroNode){
        HeroNode temp=this.heroNode;
        // 添加第一个节点
        if (temp.getNext()==null) {
            temp.setNext(heroNode);
            return;
        }
        // 添加元素
        while (true){
            // 最后一个
            if (temp.getNext()==null){
                temp.setNext(heroNode);
                break;
            }
            // 编号相同
            if (temp.getNext().getNum()==heroNode.getNum()){
                System.out.println("编号已存在，不能添加");
                break;
            }
            // 找到插入位置
            if (temp.getNext().getNum()>heroNode.getNum()){
                // 临时变量接收temp.next
                HeroNode heroNode1=temp.getNext();
                // 从中间插入链表
                temp.setNext(heroNode);
                // 接上链表的尾部
                temp.getNext().setNext(heroNode1);
                break;
            }
            // 移动指针
            temp=temp.getNext();
        }
    }
}
// 数据节点
class HeroNode{
    // 名称
    private String name;
    // 编号
    private int num;
    // 昵称
    private String nickName;
    // 下一结点
    private HeroNode next;

    public HeroNode(String name, int num, String nickName) {
        this.name = name;
        this.num = num;
        this.nickName = nickName;
    }

    public HeroNode getNext() {
        return next;
    }

    public void setNext(HeroNode next) {
        this.next = next;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "name='" + name + '\'' +
                ", num=" + num +
                ", nickName='" + nickName + '\'' +
                '}';
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public static void main(String[] args) {
        HeroNode heroNode=new HeroNode("陈彰坤",26,"陈阿坤");
        HeroNode temp=heroNode;
        temp.setNum(25);
        System.out.println(heroNode.toString());
    }
}

